<template>
  <div class="app-container">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-tabs type="border-card" class="form-tabs">
          <el-tab-pane :label="$t('common.edit_info')" class="form-tabs-tab-pane">
            <!-- 选项名 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('dictionaryItems.name')"
                  prop="name"
                >
                  <el-input v-model="form.name" :placeholder="$t('common.pleaseFill')+$t('dictionaryItems.name')" clearable />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 选项值 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('dictionaryItems.value')"
                  prop="value"
                >
                  <el-input v-model="form.value" :placeholder="$t('common.pleaseFill')+$t('dictionaryItems.value')" clearable />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 描述 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('dictionaryItems.description')"
                  prop="description"
                >
                  <el-input
                    v-model="form.description"
                    type="textarea"
                    :placeholder="$t('dictionaryItems.description_tips')"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 排序 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.sort')"
                  prop="sort"
                >
                  <el-input-number v-model="form.sort" controls-position="right" :min="1" :placeholder="$t('common.sort_tips')" />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 发布状态 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.status')"
                  prop="status"
                >
                  <el-select
                    v-model="form.status"
                    :placeholder="$t('common.pleaseSelect')+$t('common.status')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('statusVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="Number(item.value)"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <el-row :gutter="20" type="flex" justify="center" class="btn-row">
          <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
            <el-form-item>
              <el-button
                type="primary"
                :loading="submitLoading"
                :disabled="routerParams.id>0&&!checkPermission(['admin.dictionaryItems.update']) || !routerParams.id&&!checkPermission(['admin.dictionaryItems.store'])"
                @click="onSubmit"
              >{{ $t('common.submit') }}</el-button>
              <el-button @click="onCancel">{{ $t('common.cancel') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { show, update, store } from '@/api/dictionary-items'
import checkPermission from '@/utils/permission'

// export
export default {
  name: 'DictionaryItemEdit',
  components: {},
  data() {
    return {
      submitLoading: false,
      form: {
        name: '',
        value: '',
        description: '',
        sort: 999,
        status: 1
      },
      // 表单输入校验 ，通过prop="name"属性进行控制
      rules: {
        name: [
          { required: true, message: this.$t('dictionaryItems.name_tips'), trigger: 'blur' },
          { min: 1, max: 50, trigger: 'blur', message: this.$t('dictionaryItems.name_min_max_tips') }
        ],
        value: [
          { required: true, message: this.$t('dictionaryItems.value_tips'), trigger: 'blur' },
          { min: 1, max: 200, trigger: 'blur', message: this.$t('dictionaryItems.value_min_max_tips') }
        ]
      },
      // 路由参数
      routerParams: {},
      back_route_name: 'DictionaryItemsIndex',
      back_route_params: {}
    }
  },
  computed: {},
  created() {
    this.routerParams = this.$route.params
    this.back_route_params = { did: this.routerParams.did }
    this.getDetail()
  },
  methods: {
    checkPermission,
    async getDetail() {
      const { did, id } = this.routerParams
      if (id > 0) {
        await show(did, id, { showLoading: true }).then((response) => {
          this.form = response.data
        }).catch(() => {
          this.closeBack(this.back_route_name, this.back_route_params)
        })
      }
    },
    onSubmit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.submitLoading = true
          let response
          if (this.routerParams.id) {
            response = await update(this.routerParams.did, this.routerParams.id, this.form).catch(() => {})
          } else {
            response = await store(this.routerParams.did, this.form).catch(() => {})
          }
          if (response instanceof Object) {
            this.$message.success(response.message)
            this.closeBack(this.back_route_name, this.back_route_params)
          }
          this.submitLoading = false
        } else {
          this.$message.error(this.$t('common.pleaseFillRequired'))
          return false
        }
      })
    },
    onCancel() {
      this.closeBack(this.back_route_name, this.back_route_params)
    }
  }
}
</script>

<style scoped lang="scss">
</style>
